function r = roms_fix_grid ( ncfile, old_grid, coordsys )
% ROMS_FIX_GRID:  If certain roms fields are empty, try to fill them in.
%
% If the lon_[uv] or lat_[uv] fields are not present, then compute them
% and put them in.
% 
% Calls:
%     Nothing
%

if strcmp ( coordsys, 'xy' )

	[r,c] = size ( old_grid.x_rho );
	if ~isfield ( old_grid, 'x_u' )
		old_grid.x_u = (old_grid.x_rho(:,1:c-1) + old_grid.x_rho(:,2:c))/2;
	end
	if isempty ( old_grid.x_u )
		old_grid.x_u = (old_grid.x_rho(:,1:c-1) + old_grid.x_rho(:,2:c))/2;
	end
	if ~isfield ( old_grid, 'x_v' )
		old_grid.x_v = (old_grid.x_rho(1:r-1,:) + old_grid.x_rho(2:r,:))/2;
	end
	if isempty ( old_grid.x_v )
		old_grid.x_v = (old_grid.x_rho(1:r-1,:) + old_grid.x_rho(2:r,:))/2;
	end
	if ~isfield ( old_grid, 'y_u' )
		old_grid.y_u = (old_grid.y_rho(:,1:c-1) + old_grid.y_rho(:,2:c))/2;
	end
	if isempty ( old_grid.y_u )
		old_grid.y_u = (old_grid.y_rho(:,1:c-1) + old_grid.y_rho(:,2:c))/2;
	end
	if ~isfield ( old_grid, 'y_v' )
		old_grid.y_v = (old_grid.y_rho(1:r-1,:) + old_grid.y_rho(2:r,:))/2;
	end
	if isempty ( old_grid.y_v )
		old_grid.y_v = (old_grid.y_rho(1:r-1,:) + old_grid.y_rho(2:r,:))/2;
	end
	
elseif strcmp ( coordsys, 'ij' )

    %
    % Construct a grid from the dimensions.
    xi_rho_info = nc_getdiminfo ( ncfile, 'xi_rho' );
    eta_rho_info = nc_getdiminfo ( ncfile, 'eta_rho' );
    s_rho_info = nc_getdiminfo ( ncfile, 's_rho' );

    xi_rho = [1:xi_rho_info.Length];
    eta_rho = [1:eta_rho_info.Length];
    s_rho = [1:s_rho_info.Length];

    [old_grid.x_rho,old_grid.y_rho] = meshgrid ( xi_rho, eta_rho );
	[r,c] = size ( old_grid.x_rho );

	old_grid.x_u = (old_grid.x_rho(:,1:c-1) + old_grid.x_rho(:,2:c))/2;
	old_grid.x_v = (old_grid.x_rho(1:r-1,:) + old_grid.x_rho(2:r,:))/2;
	old_grid.y_u = (old_grid.y_rho(:,1:c-1) + old_grid.y_rho(:,2:c))/2;
	old_grid.y_v = (old_grid.y_rho(1:r-1,:) + old_grid.y_rho(2:r,:))/2;


	
elseif strcmp ( coordsys, 'geographic' )

	[r,c] = size ( old_grid.lon_rho );
	if ~isfield ( old_grid, 'lon_u' )
		old_grid.lon_u = (old_grid.lon_rho(:,1:c-1) + old_grid.lon_rho(:,2:c))/2;
	end
	if isempty ( old_grid.lon_u )
		old_grid.lon_u = (old_grid.lon_rho(:,1:c-1) + old_grid.lon_rho(:,2:c))/2;
	end
	if ~isfield ( old_grid, 'lon_v' )
		old_grid.lon_v = (old_grid.lon_rho(1:r-1,:) + old_grid.lon_rho(2:r,:))/2;
	end
	if isempty ( old_grid.lon_v )
		old_grid.lon_v = (old_grid.lon_rho(1:r-1,:) + old_grid.lon_rho(2:r,:))/2;
	end
	if ~isfield ( old_grid, 'lat_u' )
		old_grid.lat_u = (old_grid.lat_rho(:,1:c-1) + old_grid.lat_rho(:,2:c))/2;
	end
	if isempty ( old_grid.lat_u )
		old_grid.lat_u = (old_grid.lat_rho(:,1:c-1) + old_grid.lat_rho(:,2:c))/2;
	end
	if ~isfield ( old_grid, 'lat_v' )
		old_grid.lat_v = (old_grid.lat_rho(1:r-1,:) + old_grid.lat_rho(2:r,:))/2;
	end
	if isempty ( old_grid.lat_v )
		old_grid.lat_v = (old_grid.lat_rho(1:r-1,:) + old_grid.lat_rho(2:r,:))/2;
	end
	
else 
	
	msg = sprintf ( 'unknown coordinate system ''%s''' );
	error ( msg );

end


r = old_grid;

